package com.caval.service;

import com.caval.bean.ShoppingCart;
import com.caval.dao.CartMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CartServiceImpl implements CartService{

    @Autowired
    private CartMapper cartMapper;

    public void setCartMapper(CartMapper cartMapper) {
        this.cartMapper = cartMapper;
    }

    @Override
    public List<ShoppingCart> getCartList(int user_id) {
        return cartMapper.getCartList(user_id);
    }

    @Override
    public String addtoCart(int user_id, int game_id) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("user_id",user_id);
        map.put("game_id",game_id);
        if (this.gameExist(map)){
            return "该游戏已在购物车中！";
        }
        else {
            cartMapper.addtoCart(map);
            return "添加成功！";
        }
    }

    @Override
    public String outofCart(int user_id, int game_id) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("user_id",user_id);
        map.put("game_id",game_id);
        cartMapper.outofCart(map);
        return "已移出购物车！";
    }

    @Override
    public boolean gameExist(Map<String, Object> map) {
        return cartMapper.gameExist(map)!=0;
    }
}
